//
//  SearchViewController.m
//  CoJellyCartoon
//
//  Created by kingbw on 16/3/31.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchTableViewCell.h"
#import "SearchModel.h"
#import "NSString+Tools.h"
#import "NSDictionary+Tools.h"
#import "JMHAFNetTool.h"
#import "CJPrePlayPageVIewController.h"

#import "FinalPlayViewController.h"
@interface SearchViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) CJBaseTableView *table;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) SearchModel *searchModel;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UILabel *headLabel;

@property (nonatomic, strong) NSString *str;



@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"fanhui"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    self.page = 1;
    [self createTableView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
}

- (void)leftAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)tableviewRequest {
    
    
    NSString* rest = [self.foundModel.keyword URLEncodedString:self.foundModel.keyword];
    NSDictionary* dict = @{ @"keyword" : rest };

    NSMutableDictionary* mdic = [dict mutableCopy];

    mdic[@"_device"] = @"android";
    mdic[@"_hwid"] = @"831fc7511fa9aff5";
    mdic[@"appkey"] = @"85eb6835b0a1034e";
    mdic[@"bangumi_num"] = @"1";
    mdic[@"build"] = @"408005";
    mdic[@"main_ver"] = @"v3";
    mdic[@"page"] = [NSString stringWithFormat:@"%ld", self.page];
    mdic[@"pagesize"] = @"20";
    mdic[@"platform"] = @"android";
    mdic[@"search_type"] = @"all";
    mdic[@"source_type"] = @"0";
    mdic[@"special_num"] = @"1";
    mdic[@"topic_num"] = @"1";
    mdic[@"upuser_num"] = @"1";
    NSString* basePath = [mdic appendGetSortParameterWithSignWithBasePath:@"http://api.bilibili.cn/search?"];

    
    [JMHAFNetTool GETNetWithUrl:basePath body:nil headerFile:nil response:JMHJSON success:^(id result) {
        
        NSArray *array = [result[@"result"] objectForKey:@"video"];
        
        self.str = result[@"top_tlist"][@"video"];
        self.headLabel.text = [NSString stringWithFormat:@"   相关视频(%@)", self.str];
        
        for (NSDictionary *dic in array) {
            self.searchModel = [[SearchModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:self.searchModel];
        }
     
        [self.table reloadData];
        
        if (self.page < self.str.integerValue / 20) {
            self.page++;
            [self.table.mj_footer endRefreshing];
        }
        
//        //(@"---%ld", self.page);
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)createTableView {
    self.table = [[CJBaseTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self createHeadView];
    self.table.tableHeaderView = self.headLabel;
    
    [self.table registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"aaa"];
    
    [self.view addSubview:self.table];
    self.table.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    self.table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(tableviewRequest)];
    [self.table.mj_footer beginRefreshing];
}

//- (void)refreshAction {
//    if (self.page <= self.str.integerValue % 20) {
//        [self tableviewRequest];
//        self.page++;
//        [self.table.mj_footer endRefreshing];
//    }
//    
//}
- (void)createHeadView {
    self.headLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 30)];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"aaa"];
    
    self.searchModel = self.dataSource[indexPath.row];
    cell.searchModel = self.searchModel;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJPrePlayPageVIewController *prePlay = [[CJPrePlayPageVIewController alloc] init];
    SearchModel *search = self.dataSource[indexPath.row];
    prePlay.idString = search.aid;
    prePlay.picURL = search.pic;
    
    [self.navigationController pushViewController:prePlay animated:YES];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ScreenHeight / 6;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



@end
